package com.xckj.cycling.presenter;

import androidx.annotation.NonNull;
import com.xckj.cycling.bean.BaseBean;
import com.xckj.cycling.framework.net.RetrofitManager;
import com.xckj.cycling.global.Constant;
import com.xckj.cycling.iview.IPerfectRegisterView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import java.util.Objects;

/**
 * @Author: ChenYao
 * @Describe: 完善注册
 * @Date: 2023/3/5 23:30
 */
public class PerfectRegisterPresenter {
    private final IPerfectRegisterView perfectRegisterView;

    public PerfectRegisterPresenter(IPerfectRegisterView perfectRegisterView) {
        this.perfectRegisterView = perfectRegisterView;
    }

    public void perfectRegister(String name, String age, int gender, String weight, String city) {
        perfectRegisterView.showLoading();
        Call<BaseBean<String>> call = RetrofitManager.getInstance().getService().perfectRegister(name, age, gender, weight, "1", city);
        call.enqueue(new Callback<BaseBean<String>>() {
            @Override
            public void onResponse(@NonNull Call<BaseBean<String>> call, @NonNull Response<BaseBean<String>> response) {
                if (response.isSuccessful()) {
                    BaseBean<String> baseBean = response.body();
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        perfectRegisterView.success(baseBean.msg);
                    } else {
                        perfectRegisterView.failure(baseBean.msg);
                    }
                } else {
                    perfectRegisterView.failure("failed");
                }
                perfectRegisterView.hideDialog();
            }

            @Override
            public void onFailure(@NonNull Call<BaseBean<String>> call, @NonNull Throwable t) {
                t.printStackTrace();
                perfectRegisterView.failure("failed");
                perfectRegisterView.hideDialog();
            }
        });
    }
}
